﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaifenWeb.UtilityCls;
using CF.Data.Entity.FrmMemberEntity;
using CF.Member.DAL.Member;
using Ext.Net.MVC;

namespace CaifenWeb.Areas.BgMgr.Controllers
{
    [BgAuthorize]
    public class BadgeConfigController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int bid)
        {
            ViewData["BID"] = bid;
            return View();
        }

        public AjaxStoreResult GetAllBadge(int? start, int? limit)
        {
            int startNum = start ?? 0;
            int limitNum = limit ?? 30;
            FM_BadgetConfigDAL bdal = new FM_BadgetConfigDAL();
            List<FM_BadgeConfig> bdlist = bdal.GetAllBadge(startNum, limitNum);
            return new AjaxStoreResult(bdlist, bdlist.Count);
        }

        [HttpPost]
        public AjaxFormResult AddBadge(FM_BadgeConfig badgeCfg, string IsShow_value)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                badgeCfg.IsShow = byte.Parse(IsShow_value);
                FM_BadgetConfigDAL bdal = new FM_BadgetConfigDAL();
                bdal.Add(badgeCfg);
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public AjaxFormResult RemoveBadge(int bid)
        {
            AjaxFormResult response = new AjaxFormResult();
            FM_BadgetConfigDAL bdal = new FM_BadgetConfigDAL();
            if (bdal.Remove(bid))
            {
                response.Success = true;
                response.ExtraParams["msg"] = "删除成功！";
            }
            else
            {
                response.Success = false;
                response.ExtraParams["msg"] = "找不到数据。";
            }
            return response;
        }

        [HttpPost]
        public AjaxFormResult UpdateBadge(FM_BadgeConfig bdcfg, string IsShow_value)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                bdcfg.IsShow = byte.Parse(IsShow_value);
                FM_BadgetConfigDAL bdal = new FM_BadgetConfigDAL();
                if (bdal.Update(bdcfg.BID, bdcfg))
                {
                    response.Success = true;
                }
                else
                {
                    response.Success = false;
                    response.ExtraParams["msg"] = "找不到数据。";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
            }
            return response;
        }

        public AjaxStoreResult GetBadgeCfgForm(int bid)
        {
            FM_BadgetConfigDAL bdal = new FM_BadgetConfigDAL();
            FM_BadgeConfig bcfg = bdal.GetBadge(bid);
            return new AjaxStoreResult(bcfg);
        }
    }
}
